import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import AddEditModal from '#cpt/temp/AddEditModal'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Page 	 = $Lazy.load(()=>import('#tp/page/page-container'))
const $cache = $Lazy.file(()=>import('#private/cache-api'))
const confirm 	= $Lazy.file(()=>import('@antd/confirm'))
const ControlModal 			= $Lazy.state(()=>import('./cpt/control-modal'))
const AddModal 		= $Lazy.state(()=>import('./cpt/add-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '物价代码',		name: 'price_code' },
			{ label: '项目名称',		name: 'item_name' },
			{ label: '助记码',		name: 'qt_item_code' },
			{ label: '类型',  		name: 'type', type:'select', nameStr:'name',data:[{name:'项目',value:'2'},{name:'病理',value:'3'}] },
		],
		// 提交表单列表
		formData : [
			{ label: '物价代码',		name: 'price_code', 		half:true, disabled:true },
			{ label: '中文简称',		name: 'item_name_sort', 	half:true, disabled:true },
			{ label: '项目名称',		name: 'item_name', 			half:true, disabled:true },
			{ label: '英文简称',		name: 'item_name_en_sort', 	half:true, disabled:true },
			{ label: '英文名称',		name: 'item_name_en', 		half:true, disabled:true  },
			{ label: '检测方法',		name: 'detection_method', 	half:true, disabled:true  },
			{ label: '标准价',		name: 'standard_price', 	half:true, disabled:true  },
			// { label: '折前价',		name: 'old_price', 			half:true, required:true,
			// 	onValue: ({value}) => {
			// 		if (!parseFloat(value)) return
			// 		const price = this.formRef.getName('price')
			// 		let discount_rate = (parseFloat(price) / parseFloat(value)).toFixed(2)
			// 		this.formRef.setValue({discount_rate})
			// 	},
			// },
			{ label: '折扣率',		name: 'discount_rate', 		half:true, required:true,
				onValue: ({value}) => {
					if (!value) return
					// const old_price = this.formRef.getName('old_price') //折前价
					const old_price = this.formRef.getName('standard_price') //标准价
					let price = (parseFloat(old_price) * parseFloat(value)).toFixed(2)
					this.formRef.setValue({price})
				},
			},
			{ label: '结算价格',		name: 'price', 				half:true, required:true,
				onValue: ({value}) => {
					if (!value) return
					// const old_price = this.formRef.getName('old_price') //折前价
					const old_price = this.formRef.getName('standard_price') //标准价
					if (parseFloat(old_price)) {
						let discount_rate = (parseFloat(value) / parseFloat(old_price)).toFixed(2)
						this.formRef.setValue({discount_rate})
					}
				},
				btn:{ label:'撤回待审核',	 onClick:value=>{
					if(this.editrows.qt_price_audit_uuid){
						$http.submit(this,'qt-hospital-price-audit/del', { param:{uuids:[this.editrows.qt_price_audit_uuid]} }).then(data=>{
							$fn.msg.success('撤回待审核物价成功')
							$fn.onHideBtn.call(this, 'price',true)
							this.fetch()
						})
					}
				}}
			},
			{ label: '物价状态',		name: 'enabled', 			half:true, type:'select' },
			{ label: '排序',			name: 'sort', 				half:true, type:'number' },
		]
	}
	
	model = {}
	query = {}
	
	componentDidMount(){
		const { uuid, hosp_name, temp_name } =  $fn.query()
		this.query = { hosp_id : uuid, hosp_name: $fn.decode(hosp_name),temp_name:$fn.decode(temp_name) }
		this.rows = { hosp_id: uuid, hosp_name: $fn.decode(hosp_name) }
		this.fetch()
	}
	
	// 获取弹窗下拉框数据
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, 'enabled', ()=>{
			$cache.then( async f => {
				//物价状态
				$fn.setRow(formData, 'enabled', await f.company_state )
				//
				this.setState({ formData })
			})
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'qt-item-hospital/index')
	
	// 表单
	cols = [
		{ title: '物价代码',			dataIndex: 'price_code', 				width:100 },
		{ title: '关联物价', 		dataIndex: 'price_rel_codes', 			width:150 },
		{ title: '项目名称', 		dataIndex: 'item_name', 				width:150 },
		{ title: '英文简称', 		dataIndex: 'item_name_en_sort', 		width:150 },
		{ title: '英文名称', 		dataIndex: 'item_name_en', 				width:150 },
		{ title: '检测方法', 		dataIndex: 'detection_method_name', 	width:150 },
		{ title: '助记码', 			dataIndex: 'qt_item_code', 				...$fn.table.num },
		{ title: '标准价格(元)', 	dataIndex: 'price', 					...$fn.table.num },
		// { title: '折前价(元)', 		dataIndex: 'old_price', 				...$fn.table.num },
		{ title: '结算价格(元)', 	dataIndex: 'contract_price', 			...$fn.table.num },
		TableControls.controls.call(this, { width: 80 })
	]
	// 表格操作按钮
	table_controls = (text,rows) => {
		const { uuid, qt_temp_id, comp_id, operator_id, price_rel_codes, source_item_id, hosp_id, price_code, item_name_sort, item_name, item_name_en, item_name_en_sort, detection_method_name, price, contract_price, enabled, sort, qt_price_audit_uuid } = rows
		return [
			TableControls.edit.call(this, null, null,{
				rows: { uuid, price_code, item_name_sort, item_name, item_name_en, item_name_en_sort, detection_method: detection_method_name,
					standard_price: price, price: contract_price, enabled, sort, discount_rate: (parseFloat(contract_price) / parseFloat(price)).toFixed(2),
				},	
				select: this.getModalSelect,
				click: ()=> {
					this.editrows = rows
					$fn.disabledRows(this.state.formData, ['qt_temp_id', 'hosp_id', 'source_item_id' ], false)
					if( qt_price_audit_uuid ){
						$fn.onHideBtn.call(this, 'price',false)
					}else{
						$fn.onHideBtn.call(this, 'price',true)
					}
				},
				load: () => this.initSubmit = { source_item_id, hosp_id, price_rel_codes, source_price: price, qt_temp_id, operator_id, comp_id }
			})
		]
	}
	// 新增生成专属模板+批量修改 耿琰20220422
	// 操作按钮
	controls = () => {
		return [
			// 生成专属模板
			Confirm.call(this, '生成专属模板', 'qt-comp-item/addHospTemplate', {msg:`确认生成【${this.query.hosp_name}】的专属模板？`, query:{hosp_id: this.query.hosp_id}, onComplete:()=> $fn.dataRemove('qt-comp-template/select') } ),
			// 批量修改
			Controls.click.call(this, '批量修改',()=> this.addModal.open({hosp_id: this.query.hosp_id} )),
			// 模板导入
			Controls.click.call(this, '模板导入', ()=>{
				confirm.then(f=>{
					f({
						msg:this.query?.temp_name !== 'undefined' && this.query?.temp_name ? `该操作会覆盖【${this.query.temp_name}】模板物价是否确认?` : `该操作会影响物价，是否确认?`,
						onOk: close => {
							this.controlModalRef.open(this.rows)
							close()
						}
					})
				})
			}),
			// 导出
			Controls.exports.call(this, 'qt-item-hospital/index'),
			// 返回
			Controls.back.call(this),
		]
	}
	
	render(){
		return (
			<>
				<Page title='物价管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 编辑 */}
				{ AddEditModal.call(this,{ editApi:'qt-item-hospital/edit' }) }
				{/* 模板导入 */}
				<ControlModal onRef={ref=>this.controlModalRef=ref} onRefresh={this.fetch} />
				{/* 批量结算 */}
				<AddModal onRef={ref=>this.addModal=ref} />
			</>
		)
	}
}

export default Index